/**
 * @author hotab
 */
(function($) {
	$.fn.makeModalWindow = function(formId, url, getElementsUrl){
		$('#'+formId).dialog({modal: true,
			open: function(event, ui) {
				var tmp = $('form[name='+formId+']').serializeArray();
				$.post(getElementsUrl, tmp, function(retData, status){
					for(var key in retData){
						var value = retData[key];
						var element = $('input[name='+key+']');
						if (element.prop("type") == "radio"){
							$('input[name='+key+'][value='+value+']').attr("checked", true);
						}
						else{
							element.val(value);
						}
					}
							jQuery('form[name='+formId+']').validationEngine('attach');
							if ($('input[name=isbrak]:checked').val() == 0) {
								$('input[name=numbersp], input[name=kolbrak]').attr("disabled", true);
							}
				});
				},
			show: 'fade',
			hide: 'fade',
			resizable: false,
			width: 400,
			close: function(event, ui) { jQuery('form[name='+formId+']').validationEngine('hideAll'); },
			buttons: {"Зберегти": function(e){
										switch (formId) {
											case 'stat5dialog':
												var subjSum = 0;
												$('#subjList input').each(function(index, element){
													subjSum = subjSum+parseInt($(this).val());
												});
												if (subjSum == $('input[name=kolnotcame]').val()) {
													if (jQuery('form[name='+formId+']').validationEngine('validate') == true){
														jQuery('form[name='+formId+']').validationEngine('hideAll');
														var arr = $('form[name='+formId+']').serializeArray();
														$.post(url, arr, function(retData, status){
															if (status == "success"){
																$('#main button').getStatFillInfo("main", "check_stat_filling/", "completed");
															}
														});
														$('#'+formId).dialog("close");
													}													
												} else {
													alert('Не співпадає кількість відсутніх!');
												}												
											break
											default:
												if (jQuery('form[name='+formId+']').validationEngine('validate') == true){
													jQuery('form[name='+formId+']').validationEngine('hideAll');
													var arr = $('form[name='+formId+']').serializeArray();
													$.post(url, arr, function(retData, status){
														if (status == "success"){
															$('#main button').getStatFillInfo("main", "check_stat_filling/", "completed");
														}
													});
													$('#'+formId).dialog("close");
												}
											break
										}
									}
					}
		});	
	}
})(jQuery);